
<template>
  <div class="login-box">
    <h2 class="form-title">登    录</h2>

    <form @submit.prevent="handleLogin">
      <div class="form-item">
        <label for="username">用户名</label>
        <input
            type="text"
            id="username"
            v-model="username"
            required
            class="input-style"
            placeholder="请输入用户名/电话"
        />
      </div>

      <div class="form-item">
        <label for="password">密码</label>
        <input
            type="password"
            id="password"
            v-model="password"
            required
            class="input-style"
            placeholder="请输入密码"
        />
      </div>

      <button type="submit" class="login-btn" :disabled="isLoading">
        {{ isLoading ? '登录中...' : '登录' }}
      </button>

      <div class="action-links">
        <a @click="$router.push('/change-password')" class="link-text">忘记密码？</a>
        <a @click="$router.push('/register')" class="link-text">注 册</a>

      </div>

      <div v-if="errorMessage" class="error-msg">
        {{ errorMessage }}
      </div>
    </form>
  </div>
</template>

<script>
import { login } from '@/api/auth';

export default {
  data() {
    return {
      username: '',
      password: '',
      errorMessage: '',
      isLoading: false
    };
  },
  methods: {
    async handleLogin() {
      if (!this.username || !this.password) {
        this.errorMessage = '请输入用户名和密码';
        return;
      }

      this.isLoading = true;
      this.errorMessage = '';

      try {
        const res = await login({
          username: this.username,
          password: this.password
        });

        if (res.code === 0) {
          localStorage.setItem('token', res.data.token);
          localStorage.setItem('userRole', res.data.role); // 👈 保存角色
          this.$router.push('/home');
        }

        else {
          this.errorMessage = res.msg || '登录失败';
        }
      } catch (error) {
        console.error('登录错误:', error);
        if (error.response) {
          this.errorMessage = error.response.data.msg || '请求失败';
        } else if (error.request) {
          this.errorMessage = '服务器无响应';
        } else {
          this.errorMessage = '请求发送失败';
        }
      } finally {
        this.isLoading = false;
      }
    }
  }
};
</script>

<style scoped>
/* 保持原有的样式不变 */
.login-box {
  background: linear-gradient(to bottom right, rgba(13, 13, 13, 0.55) 0%, white 60%, rgba(13, 13, 13, 0.55) 100%);
  width: 380px;
  height: 490px;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.form-title {
  color: #2c3e50;
  text-align: center;
  margin-top: 20px;
  font-size: 36px;
}

.form-item {
  margin-bottom: 25px;
  width: 100%;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  color: #34495e;
  font-weight: 500;
}

.input-style {
  width: 100%;
  height: 42px;
  padding: 0 15px;
  border: 2px solid #bdc3c7;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s;
  box-sizing: border-box;
}

.input-style:focus {
  border-color: #3498db;
  outline: none;
}

.login-btn {
  width: 100%;
  height: 45px;
  background: #28292CFF;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1.5rem;
  cursor: pointer;
  transition: background 0.3s;
  margin-top: 15px;
}

.login-btn:hover:not(:disabled) {
  background: #000000FF;
}

.login-btn:disabled {
  background: #cccccc;
  cursor: not-allowed;
}

.action-links {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  width: 100%;
}

.link-text {
  color: #2c3e50;
  cursor: pointer;
  font-size: 0.95rem;
  text-decoration: underline;
  transition: color 0.3s;
}

.link-text:hover {
  color: #e74c3c;
}

.error-msg {
  color: #e74c3c;
  text-align: center;
  margin-top: 15px;
  font-weight: 500;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .form-title {
    font-size: 36px;
  }

  .input-style {
    height: 38px;
  }

  .login-btn {
    height: 42px;
    font-size: 1rem;
  }
}

@media (max-width: 480px) {
  .action-links {
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }
}
</style>